<template>
    <div> 
         <Layout>
      <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
        <FormItem label="用户名" prop="teacherName" class="Position">
            <Input v-model="formValidate.teacherName" placeholder="输入你的用户名"></Input>
        </FormItem>
         <FormItem label="密码" prop="teacherPwd">
            <Input v-model="formValidate.teacherPwd" placeholder="输入你的密码"></Input>
        </FormItem>
         <FormItem label="教师工号" prop="teacherId">
            <Input v-model="formValidate.teacherId" type="number"  placeholder="你的教师工号"></Input>
        </FormItem>
            <FormItem label="E-mail" prop="mail">
            <AutoComplete
        v-model="formValidate.mail"
        @on-search="handleSearch2"
        placeholder="输入你的Email"
        style="width:100%">
        <Option v-for="item in data2" :value="item" :key="item">{{ item }}</Option>
              </AutoComplete>
        </FormItem>
               <FormItem label="电话" prop="mobile">
            <Input v-model="formValidate.mobile" placeholder="输入你的电话"></Input>
        </FormItem>
        <FormItem label="性别" prop="gender">
            <RadioGroup v-model="formValidate.gender">
                <Radio label="男">男</Radio>
                <Radio label="女">女</Radio>
            </RadioGroup>
        </FormItem>
        <FormItem label="专业" prop="major">
            <Select v-model="formValidate.major">
                <Option value="通信工程">通信工程</Option>
                <Option value="软件工程">软件工程</Option>
                <Option value="计算机科学">计算机科学</Option>
                <Option value="大数据分析">大数据分析</Option>
            </Select>
        </FormItem>
           <FormItem label="课程" prop="project">
            <Input v-model="formValidate.project" placeholder="输入你所教的科目">
            </Input>
        </FormItem>
        <FormItem>
            <Button type="primary" @click.once="handleSubmit('formValidate')">提交</Button>
            <Button @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
        </FormItem>
    </Form>
     </Layout>
  </div>
</template>
<script>
    const validatePhone = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('手机号不能为空'));
            } else if (!/^1[34578]\d{9}$/.test(value)) {
                callback('手机号格式不正确');
            } else {
                callback();
            }   
        };
    function addZero(val) {
     if(val<10){
         return '0'+val
     }else{
         return val
     }
 }
 
    export default { 
        data () {
            return {
                     data2: [],
                formValidate: {
                    teacherName: '',
                    teacherId:'',
                    teacherPwd:'',
                    mobile:'',
                    mail: '',
                    grade:0,
                    gender: '',
                    major: '',
                    project:'',
                    date: '',
                },
                ruleValidate: {
                    teacherName: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],
                      teacherId : [
                        { required: true, message: '请输入教师工号', trigger: 'blur' },
                         {min:6,max:20,message:'密码长度为6到20位',trigger:'change'},
                         {pattern:/^[A-Z-z0-9]+$/,message:'只能是数字或字母'}
                    ],
                      teacherPwd: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ],
                    mail: [
                        { required: true, message: '邮箱不能为空', trigger: 'blur' },
                    ],
                    mobile: [
                    { required: true,validator:validatePhone,trigger:'blur'},
                    ],
                    project: [
                        { required: true, message: '输入你所教的科目', trigger: 'blur' }
                    ],
                    gender: [
                        { required: true, message: '请选择你的性别', trigger: 'change' }
                    ],
                    major: [
                         { required: true, message: '请选择你的专业', trigger: 'change' }    
                    ],
                     teacherId: [
                        { required: true, message: '请输入你的教师工号', trigger: 'blur' },
                         {len:10,message:'教师工号长度为10位',trigger:'change'},
                         {pattern:/^[A-Z-z0-9]+$/,message:'只能是数字或字母'}
                    ],
                }
            }
        },
        methods: {
              handleSearch2 (value) {
                this.data2 = !value || value.indexOf('@') >= 0 ? [] : [
                    value + '@qq.com',
                    value + '@sina.com',
                    value + '@163.com'
                ];
            },
            handleSubmit (name) {
            var data=new Date()
            var year=data.getFullYear()
            var month=addZero(data.getMonth())
            var day=addZero(data.getDate())
            var setTime=year+'-'+month+'-'+day
            this.formValidate.date=setTime
           var teacherNameMsg={teacherName:this.formValidate.teacherName}
                 
                this.$refs[name].validate((valid) => {
                    if (valid) {
                   this.$axios({
                   method:"post",
                   url:"http://"+this.urlpath+"/FindOneT",
                   data:teacherNameMsg
               }).then((res)=>{                                                                     
               if(res.data.teacherName!==this.formValidate.teacherName){
                   this.$axios({
                   method:"post",
                   url:"http://"+this.urlpath+"/CreateNewTeacher",
                   data:this.formValidate
               }).then((res)=>{
                   
               })
                     this.$Message.success('提交成功!');
                   }
                else if (res.data.teacherName===this.formValidate.teacherName){
                       this.$Message.error('用户名已存在!')
                   } 
                else{
                    this.$Message.error('用户名已存在!')
                   }                  
               })
                } else {
                        this.$Message.error('内容有误!');
                    }
                })
            },
            handleReset (name) {
                this.$refs[name].resetFields();
            }
        }
    }
</script>
<style scoped>
  .Position{
    margin-top:30px; 
  }
</style>
